package com.wenhuitec.android.tang.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.entity.Orders;
import com.wenhuitec.android.tang.sysui.adapter.PickAdapter;
import com.wenhuitec.android.tang.ui.adapter.OrderAdapter;
import com.wenhuitec.android.tang.ui.dialog.OrdersDialog;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Activity3 extends AppCompatActivity {
    private Spinner spinner;
    private Spinner typeSpinner;
    private Spinner shopSpinner;

    ArrayAdapter<String> adapter;
    ArrayAdapter<AddrTree> adapter2;
    private List<AddrTree> list = new ArrayList<>();
    private List<AddrTree> shopList = new ArrayList<>();
    private List<String> typeList = new ArrayList<>();
    private Context mContext;

    private List<AddrTree> addrTreeList;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mContext = this;
        spinner = (Spinner) findViewById(R.id.spinner);
        shopSpinner = (Spinner) findViewById(R.id.spinner2);
        typeSpinner = (Spinner) findViewById(R.id.spinner3);
        recyclerView = findViewById(R.id.ordersRv);


        List<Orders> ordersList = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            Orders orders = new Orders();
            orders.setOrders(i + "zt");
            orders.setNumber(new BigDecimal(i));
            orders.setPrice(new BigDecimal(i));
            ordersList.add(orders);
        }

        OrderAdapter orderAdapter = new OrderAdapter(mContext, ordersList, orders -> {
            startActivity(new Intent(mContext, OrdersDialog.class));
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(orderAdapter);
        spinner.setPrompt("请选择院区");
        addrTreeList = SystemInit.getInstance(mContext).getAddrTreeList();
        initAddress();
        typeSpinner = (Spinner) findViewById(R.id.spinner3);
        typeSpinner.setPrompt("请选择类型");
        initType();
    }

    private void initAddress() {
        list = new ArrayList<>();
        for (AddrTree goo : addrTreeList) {
            list.add(goo);
        }
        adapter2 = new ArrayAdapter<AddrTree>(this, android.R.layout.simple_list_item_1, list);
        spinner.setAdapter(adapter2);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                initShop(list.get(position).getId());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }


    private void initShop(String id) {
        shopList.clear();
        AddrTree addrTree = getAddrTree(addrTreeList, id);
        for (AddrTree goo : addrTree.getChildren()) {
            shopList.add(goo);
        }
        ArrayAdapter<AddrTree> typeAdapter = new ArrayAdapter<AddrTree>(this, android.R.layout.simple_list_item_1, shopList);
        shopSpinner.setAdapter(typeAdapter);
        shopSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initType() {
        typeList = new ArrayList<>();
        typeList.add("现金收款");
        typeList.add("点餐");
        ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, typeList);
        typeSpinner.setAdapter(typeAdapter);
        typeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private AddrTree getAddrTree(List<AddrTree> addrTreeList, String id) {
        for (AddrTree goo : addrTreeList) {
            if (goo.getId().equals(id)) {
                return goo;
            }
        }
        return null;
    }


}
